﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;

namespace Mystery.SharePoint
{
    public static class MetadataExtensions
    {
        private const string MetadataProviderKey = "SPM.MetadataProvider";

        /// <summary>
        /// Gets the metadata for a site
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="site"></param>
        /// <returns></returns>
        public static T GetMetadata<T>(this SPSite site) where T : SiteMetadata
        {
            return site.RootWeb.CreateObjectFromPropertyString<T>(MetadataProviderKey, site);
        }

        /// <summary>
        /// Sets the name of the metadata provider to use. Depending on the return value, you may have to update
        /// the root site property bag.
        /// </summary>
        /// <param name="site"></param>
        /// <param name="type">Type representing the provider</param>
        /// <returns>True if rootweb property bag was updated</returns>
        public static bool SetMetadataProvider(this SPSite site, Type type)
        {
            return site.RootWeb.SetLocalizableProperty(MetadataProviderKey, type.AssemblyQualifiedName);
        }
    }
}
